package com.feel.mall.orderservice.service;

import com.feel.mall.common.bean.Cart;
import com.feel.mall.common.bean.Goods;
import com.feel.mall.common.exception.FeelException;
import com.feel.mall.common.service.CartService;
import com.feel.mall.common.service.GoodsService;
import com.feel.mall.orderservice.mapper.CartMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 刘永钦
 * @Description:
 */
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @DubboReference
    private GoodsService goodsService;

    @Override
    public Boolean addInfo(Cart cart) throws FeelException {
        // 查询购物车中是否纯在该记录
        Cart localCart = cartMapper.getByUserIdAndGoodsId(cart.getUserId(), cart.getGoodsId());
        if (localCart == null) {
            cart.setCount(1);
            // 直接添加
            return cartMapper.addInfo(cart) == 1;
        }
        // 判断商品的库存是否足够
        Goods goods = goodsService.selectById(localCart.getGoodsId());
        if (localCart.getCount() + 1 > goods.getCount()) {
            throw new FeelException("库存不足");
        }

        // 修改流程
        localCart.setCount(localCart.getCount() + 1);
        return cartMapper.updateInfo(localCart) == 1;
    }

    @Override
    public Boolean updateInfo(Cart cart, Integer userId) throws FeelException {
        // 判断该购物车信息是否存在
        Cart c = cartMapper.getById(cart.getId());
        if (c == null) {
            throw new FeelException("该记录不存在，无法修改");
        }

        // 判断被修改购物车是否属于当前用户
        if (!c.getUserId().equals(userId)) {
            throw new FeelException("权限错误");
        }

        //判断库存是否充足
        Goods goods = goodsService.selectById(c.getGoodsId());
        if (cart.getCount() != null && cart.getCount() > goods.getCount() ) {
            throw new FeelException("库存不足");
        }

        return cartMapper.updateInfo(cart) == 1;
    }

    @Override
    public Integer getCartNumber(Cart cart) {
        Cart localCart = cartMapper.getByUserIdAndGoodsId(cart.getUserId(), cart.getGoodsId());
        if (localCart == null) {
            return 0;
        } else {
            return localCart.getCount();
        }
    }

    @Override
    public Boolean delInfo(Integer id, Integer userId) throws FeelException {
        // 判断该购物车信息是否存在
        Cart cart = cartMapper.getById(id);
        if (cart == null) {
            throw new FeelException("该记录不存在，无法修改");
        }

        // 判断被删除的购物车是否属于当前用户
        if (!cart.getUserId().equals(userId)) {
            throw new FeelException("权限错误");
        }

        // 执行删除
        return cartMapper.delInfo(id) == 1;
    }

    @Override
    public List<Cart> getByCondition(Cart condition) {
        List<Cart> list = cartMapper.getByCondition(condition);

        //调用goodsService查询对应商品
        list.forEach(cart -> {
            Goods goods = goodsService.selectById(cart.getGoodsId());
            cart.setGoods(goods);
        });
        return list;
    }

    @Override
    public List<Cart> getByIdList(Integer[] cartId, Integer userId) throws FeelException {
        List<Cart> list = cartMapper.getByIdList(cartId);
        //调用goodsService查询对应商品
        for (Cart cart : list) {
            if (!cart.getUserId().equals(userId)) {
                throw new FeelException("权限错误");
            }
            Goods goods = goodsService.selectById(cart.getGoodsId());
            cart.setGoods(goods);
        }

        return list;
    }
}
